package com.springboot.jdk.concurrency;

import java.util.concurrent.Exchanger;

/**
 * @author: zr
 * @date: 2021-10-09
 */
public class ExchangeDemo {

    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();

        new Thread() {

            @Override
            public void run() {
                try {
                    String data1 = exchanger.exchange("线程1的数据..................");
                    System.out.println(Thread.currentThread() + "线程1获取到线程2交互过来的数据:" + data1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread() {

            @Override
            public void run() {
                try {
                    String data2 = exchanger.exchange("线程2的数据...............");
                    System.out.println(Thread.currentThread() + "线程2获取到线程1交互过来的数据:" + data2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
